﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports CodeMatrix.Context

' Created by Boonyarit Yingvoragan
' Created date 31/10/2553
Public Class GroupUser

    Public Shared Function Delete(ByVal tbl As String, ByVal pid As String, ByVal cid As String) As Int32

        Dim affectedRecord = -1
        Using c As New SqlConnection(CodeMatrix.Utilities.CodeHelper.ConnectionString)

            Dim cmd As New SqlCommand()
            cmd.CommandText = "EXEC  [dbo].[USP_GenericDelete] @tbl,@pid,@cid;"
            cmd.Connection = c

            cmd.Parameters.Add(New SqlParameter("@tbl", tbl))
            cmd.Parameters.Add(New SqlParameter("@pid", pid))
            cmd.Parameters.Add(New SqlParameter("@cid", cid))

            If c.State <> ConnectionState.Open Then
                c.Open()
            End If
            affectedRecord = cmd.ExecuteNonQuery()

            ' =========================================================
            ' Log command
            ' =========================================================
            Dim e As New OrderedDictionary()
            e.Add(pid, cid)
            GenericHelper.GenerateScript(GenerateScriptLogType.Delete, LogMenuType.group_user, e)
        End Using

        Return affectedRecord

    End Function

    Public Shared Function GetData(ByVal group_user_name As String, _
                                ByVal group_user_type As Int32, _
                                ByVal group_user_status As Int32, _
                                ByVal group_user_password_expire As String, _
                                ByVal department_code As Int32, _
                                ByVal ctx_group_user_type As Int32, _
                                ByVal ctx_department_code As Int32) As DataTable

        ' =========================================================================
        ' Get table
        ' =========================================================================
        Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            "exec USP_Filter_group_user @group_user_name, @group_user_type, @group_user_status, @group_user_password_expire, @department_code, @ctx_group_user_type, @ctx_department_code", _
             New SqlParameter("@group_user_name", group_user_name), _
             New SqlParameter("@group_user_type", group_user_type), _
             New SqlParameter("@group_user_status", group_user_status), _
             New SqlParameter("@group_user_password_expire", group_user_password_expire), _
             New SqlParameter("@department_code", department_code), _
             New SqlParameter("@ctx_group_user_type", ctx_group_user_type), _
             New SqlParameter("@ctx_department_code", ctx_department_code) _
            )

        'tbl = StpDepartment.ArrangeData(tbl)

        Return tbl

    End Function

    Public Shared Function GetDataForReport(ByVal group_user_type As Int32, _
                                ByVal department_code As String, _
                                ByVal ctx_group_user_type As Int32, _
                                ByVal ctx_department_code As Int32) As DataTable

        If department_code = "0" Then
            Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            "exec USP_List_group_user1 @group_user_type,  @department_code, @ctx_group_user_type, @ctx_department_code", _
             New SqlParameter("@group_user_type", group_user_type), _
             New SqlParameter("@department_code", department_code), _
             New SqlParameter("@ctx_group_user_type", ctx_group_user_type), _
             New SqlParameter("@ctx_department_code", ctx_department_code) _
            )
            Return tbl
        Else
            Dim splitString() As String

            splitString = Split(department_code, ",")

            Dim allTable As New DataTable

            allTable.Columns.Add("group_user_code")
            allTable.Columns.Add("status")
            allTable.Columns.Add("group_user_type_name")
            allTable.Columns.Add("SeqNo")
            allTable.Columns.Add("department_name")
            allTable.Columns.Add("group_user_name")
            allTable.Columns.Add("group_user_password_expire")
            allTable.Columns.Add("department_code")
            allTable.Columns.Add("department_parent")
            allTable.Columns.Add("master_department_level")

            'Dim iReportRowCount As Integer = 0

            For i As Integer = 0 To splitString.Length()
                Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
                "exec USP_List_group_user1 @group_user_type,  @department_code, @ctx_group_user_type, @ctx_department_code", _
                 New SqlParameter("@group_user_type", group_user_type), _
                 New SqlParameter("@department_code", splitString(i)), _
                 New SqlParameter("@ctx_group_user_type", ctx_group_user_type), _
                 New SqlParameter("@ctx_department_code", ctx_department_code) _
                )

                'If iReportRowCount = 0 Then

                '    For x As Integer = 0 To tbl.Columns.Count - 1
                '        allTable.Columns.Add(tbl.Columns.Item(x))
                '    Next

                'End If


                If tbl.Rows.Count <> 0 Then
                    For a As Integer = 0 To tbl.Rows.Count - 1
                        allTable.Rows.Add(tbl.Rows(a))
                    Next
                End If
                'iReportRowCount = iReportRowCount + 1
            Next
            Return allTable
        End If

    End Function

End Class
